import '../scss/common.scss';
import { IndexModel } from '../models/index';

class App {
    constructor($, options) {
        this.$app = $('<div id="app">');
        this.swiper = options.swiper;
        this.phone = options.phone;
        this.field = options.field;
        this.cache = null;

        this.init();
    }

    async init() {
        await this.getDatas();
        this.render();
    }

    async getDatas() {
        const indexModel = new IndexModel();

        //方式一
        //models文件夹下面的index.js请求数据用Promise时，这里就这样写
        // await indexModel.getDatas({
        //     swiper: this.swiper,
        //     phone: this.phone,
        //     field: this.field
        // }).then(res => {
        //     this.cache = {
        //         phoneDatas: res.phone_data,
        //         fieldDatas: res.field_data,
        //         swiperDatas: res.swiper_data
        //     }
        // });

        //方式二
        const data = await indexModel.getDatas({
            swiper: this.swiper,
            phone: this.phone,
            field: this.field
        });
        this.cache = {
            phoneDatas: data.phone_data || null,
            fieldDatas: data.field_data || null,
            swiperDatas: data.swiper_data || null
        };
    }

    filterDatas(field) {
        return this.cache.phoneDatas.filter((item, idx) => {
            switch (field) {
                case 'new'://手机上新
                    return item.new == 1;
                    break;
                case 'valuable'://超值手机
                    return item.most_value == 1;
                    break;
                case 'recom'://官方推荐
                    return item.recom == 1;
                    break;
                default:
                    break;
            }
        })
    }
}

export { App };